package com.czmh.etc.onlinepubsys.controller.mingchuang;

import com.alibaba.fastjson.JSON;
import com.czmh.etc.onlinepubsys.constants.GlobalStatusCode;
import com.czmh.etc.onlinepubsys.json.mingchuang.JSONResult;
import com.czmh.etc.onlinepubsys.request.mingchuang.SendVerifyCodeRequest;
import com.czmh.etc.onlinepubsys.utils.AliSmsUtil;
import com.czmh.etc.onlinepubsys.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/{version}/api/message")
@Api(tags = "短信接口【新】- 铭创")
@Slf4j
public class MessageController {
    @Autowired
    private RedisUtil redisUtil;

    @PostMapping("/sendVerifyCode")
    @ApiOperation("发送验证码")
    public JSONResult sendVerificationCode(@RequestBody SendVerifyCodeRequest req) {
        log.info("发送验证码:{}", JSON.toJSONString(req));
        JSONResult result = new JSONResult();
        if (redisUtil.hasKey(req.getPhone())) {
            result.setCode(JSONResult.FAIL);
            result.setMessage("短信发送频繁，请稍后重试！");
        } else {
            String code = AliSmsUtil.sendPhoneVerificationcode(req.getPhone());
            if (!GlobalStatusCode.SUCCESS.equals(code)) {
                result.setCode(JSONResult.SUCCESS);
            }
        }
        return result;
    }
}
